import { _decorator, CCFloat, Component, EventKeyboard, EventTouch, input, Input, KeyCode, math, Sprite, v3, Vec3 } from 'cc'
import { VirtualInput } from './VirtualInput'
const { ccclass, property } = _decorator

@ccclass('UIJoyStick')
export class UIJoyStick extends Component {
    // 手指
    @property(Sprite)
    thumbnail: Sprite = null // 遥杆
    @property(Sprite) // 遥杆背景
    joystickBg: Sprite = null
    @property(CCFloat)// 遥杆拖拽半径
    radius: number = 130
    initJoyStickBgPosition: Vec3 = v3()
    // 遥杆范围
    start() {
        this.joystickBg.node.active = false
        this.node.on(Input.EventType.TOUCH_START, this.onTouchStart, this)
        this.node.on(Input.EventType.TOUCH_MOVE, this.onTouchMove, this)
        this.node.on(Input.EventType.TOUCH_END, this.onTouchEnd, this)
        this.node.on(Input.EventType.TOUCH_CANCEL, this.onTouchEnd, this)
        input.on(Input.EventType.KEY_DOWN, this.onKeyDown, this)
        input.on(Input.EventType.KEY_UP, this.onKeyUp, this)
        //

    }
    onKeyDown(e: EventKeyboard) {

        switch (e.keyCode) {
            case KeyCode.KEY_A:
                VirtualInput.horizontal = -1
                break
            case KeyCode.KEY_D:
                VirtualInput.horizontal = 1
                break
            case KeyCode.KEY_W:
                VirtualInput.vertical = 1
                break
            case KeyCode.KEY_S:
                VirtualInput.vertical = -1

                break

            default:
                break
        }

    }
    onKeyUp(event: EventKeyboard) {
        switch (event.keyCode) {
            case KeyCode.KEY_W:
                VirtualInput.vertical = 0
                break
            case KeyCode.KEY_S:
                VirtualInput.vertical = 0
                break
            case KeyCode.KEY_A:
                VirtualInput.horizontal = 0
                break
            case KeyCode.KEY_D:
                VirtualInput.horizontal = 0
                break
        }
    }

    onTouchStart(e: EventTouch) {
        console.log('onTouchStart')
        this.joystickBg.node.active = true
        let { x, y } = e.touch.getUILocation()
        this.joystickBg.node.setWorldPosition(v3(x, y, 0))
        this.initJoyStickBgPosition = this.joystickBg.node.worldPosition.clone()
    }
    setThumbnalPosition(e) {
        let { x, y } = e.touch.getUILocation()
        const deltaX = x - this.initJoyStickBgPosition.x
        const deltaY = y - this.initJoyStickBgPosition.y
        let finalX, finalY
        // 确保偏移量不超过半径
        const limitedDeltaX = Math.min(Math.abs(deltaX), this.radius) * (deltaX > 0 ? 1 : -1)
        const limitedDeltaY = Math.min(Math.abs(deltaY), this.radius) * (deltaY > 0 ? 1 : -1)
        // 计算最终的坐标
        finalX = this.initJoyStickBgPosition.x + limitedDeltaX
        finalY = this.initJoyStickBgPosition.y + limitedDeltaY
        this.thumbnail.node.setWorldPosition(v3(finalX, finalY, 0))
    }
    onTouchMove(e: EventTouch) {
        // 获取当前触摸点
        let { x: touchX, y: touchY } = e.touch.getUILocation()
        // 将位置转换到 joystickBg 的本地坐标
        const localPosition = this.joystickBg.node.inverseTransformPoint(v3(), v3(touchX, touchY, 0))
        // 计算并设置 thumbnail 的位置

        let len = localPosition.length()
        localPosition.normalize()
        let thumbnailPosition = v3() // 存放 Thumbnail精灵的位置
        Vec3.scaleAndAdd(thumbnailPosition, v3(), localPosition, math.clamp(len, 0, this.radius))

        this.thumbnail.node.setPosition(thumbnailPosition)
        // // 更新 VirtualInput 数据
        VirtualInput.horizontal = localPosition.x
        VirtualInput.vertical = localPosition.y

    }
    onTouchEnd(e: EventTouch) {
        this.joystickBg.node.active = false
        VirtualInput.horizontal = 0
        VirtualInput.vertical = 0
        this.thumbnail.node.worldPosition = this.initJoyStickBgPosition.clone()
    }
    update(deltaTime: number) {

    }
    protected onDestroy(): void {
        this.node.off(Input.EventType.TOUCH_START, this.onTouchStart, this)
        this.node.off(Input.EventType.TOUCH_MOVE, this.onTouchMove, this)
        this.node.off(Input.EventType.TOUCH_END, this.onTouchEnd, this)

    }
}

